package com.aisino.controller;

import com.aisino.entity.*;
import com.aisino.util.BigDecimalUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aisino.mapper.*;
import com.aisino.service.AutoService;
import com.aisino.service.HeBingService;
import com.aisino.util.HandleDataUtil;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.*;

@RequestMapping("/cgzb")
@Controller
public class AutoHeBingChaiFenController {
    @Resource
    private HeBingMapper heBingMapper;
    @Resource
    private SelectMapper selectMapper;
    @Resource
    private HeBingService heBingService;
    @Resource
    private AutoService autoService;

    @Resource
    private TSysorderlineMapper tSysorderlineMapper;

    @Resource
    private TSysorderheadMapper tSysorderheadMapper;

    @Resource
    private TOrderDataPcDjMapper tOrderDataPcDjMapper;

    static final String maxMoney = "100000";

    static final BigDecimal max = new BigDecimal(maxMoney);

    static final BigDecimal maxChaiFenJe = new BigDecimal("99999");


    @RequestMapping("/ChuShiDataToDaiKaiPiao")
    @ResponseBody
    public Object autoHeBingChaiFen(@RequestBody List<Map<String,String>>list) throws Exception {
        System.out.println(list);
        for(int i=0;i<list.size();i++){
            autoService.autoChaiFenHeBing(list.get(i).get("DJHM"));
            // 将此发票状态更新为DKFPBS
            TOrderDataPcDj tOrderDataPcDj=new TOrderDataPcDj();
            tOrderDataPcDj.setDJHM(list.get(i).get("DJHM"));
            tOrderDataPcDj.setDKFPBS("1");
            tOrderDataPcDjMapper.updateByPrimaryKeySelective(tOrderDataPcDj);
        }

        Json result=new Json(true,0,"生成成功!");
        return result;
    }
    @RequestMapping("/ChaiFenHang")
    @ResponseBody
    public Object ChaiFenHang(@RequestBody Map<String,String>map) throws Exception {
        String OHID = map.get("OHID");
        String[] OLIDs = map.get("OLID").split(",");

        // 查出商品头
        TSysorderheadWithBLOBs tSysorderheadWithBLOBs = tSysorderheadMapper.selectByPrimaryKey(OHID);

        // 查出商品行
        List<TSysorderline> tSysorderlines = tSysorderlineMapper.selectOrderLine(OHID);

        List<TSysorderline> oldList = new ArrayList<>();
        List<TSysorderline> newList = new ArrayList<>();

        for(int i=0;i<tSysorderlines.size();i++) {
            // 是否包含
            if (Arrays.asList(OLIDs).contains(tSysorderlines.get(i).getOLID())) {
                oldList.add(tSysorderlines.get(i));
            }else{
                newList.add(tSysorderlines.get(i));
            }
        }
        // 储存数据
        insertData(oldList, tSysorderheadWithBLOBs);
        insertData(newList, tSysorderheadWithBLOBs);

        // 删除原有数据
        tSysorderheadMapper.deleteByPrimaryKey(OHID);
        tSysorderlineMapper.delectByCondition(OHID);

        // 然后将此单据设为已拆分
        TOrderDataPcDj tOrderDataPcDj = tOrderDataPcDjMapper.selectByPrimaryKey(tSysorderheadWithBLOBs.getDJHM());
        tOrderDataPcDj.setCFBS("1");
        tOrderDataPcDjMapper.updateByPrimaryKeySelective(tOrderDataPcDj);

        Json result=new Json(true,0,"拆分成功!");

        return result;
    }

    public void insertData(List<TSysorderline> oldList,TSysorderheadWithBLOBs tSysorderheadWithBLOBs) {
        // 计算价税合计等再插入
        BigDecimal JSHJ = new BigDecimal("0");
        BigDecimal HJJE = new BigDecimal("0");
        BigDecimal HJSE = new BigDecimal("0");
        String newOHID = "123" + UUID.randomUUID().toString().replace("-", "");
        for(int i=0;i<oldList.size();i++) {
            oldList.get(i).setOLID("123"+UUID.randomUUID().toString().replace("-", ""));
            oldList.get(i).setSPXH(i+1);
            oldList.get(i).setOHID(newOHID);
            JSHJ = BigDecimalUtil.add(oldList.get(i).getJE(), JSHJ);
            HJJE = BigDecimalUtil.add(oldList.get(i).getBHSJE(), HJJE);
            HJSE = BigDecimalUtil.add(oldList.get(i).getSE(), HJSE);
            // 插入
            tSysorderlineMapper.insertSelective(oldList.get(i));
        }
        tSysorderheadWithBLOBs.setOHID(newOHID);
        tSysorderheadWithBLOBs.setJSHJ(JSHJ);
        tSysorderheadWithBLOBs.setHJJE(HJJE);
        tSysorderheadWithBLOBs.setHJSE(HJSE);
        // 插入商品头
        tSysorderheadMapper.insertSelective(tSysorderheadWithBLOBs);
    }

    @RequestMapping("/HeBingBuTongFaPiao")
    @ResponseBody
    public Object HeBingBuTongFaPiao() {
        // 首先查询出来不同发票头是否相同
        String[]OHID=null;
        List<TSysorderheadWithBLOBs> list = new ArrayList<>();

        TSysorderheadWithBLOBs tSysorderheadWithBLOBsTemp = new TSysorderheadWithBLOBs();

        for(int i=1;i<OHID.length;i++) {
            TSysorderheadWithBLOBs tSysorderheadWithBLOBs = tSysorderheadMapper.selectByPrimaryKey("");
            list.add(tSysorderheadWithBLOBs);
        }

        // 遍历比较对象是否一样
        for (int i=0;i<list.size();i++) {
            if (!tSysorderheadWithBLOBsTemp.equals(list.get(i))) {
                return new Json(true,"0","合并失败，所选发票抬头信息不一致");
            }
        }

        Json result=new Json(true,0,"合并成功!");
        return result;
    }

}
